package com.lotus.lqmp.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lotus.idaas.sdk.common.UserInfoContext;
import com.lotus.lqmp.service.common.utils.UuidUtil;
import com.lotus.lqmp.service.controller.vo.req.ReviewPlanQueryReq;
import com.lotus.lqmp.service.dao.ReviewPlanMapper;
import com.lotus.lqmp.service.dao.entity.ReviewPlanDo;
import com.lotus.lqmp.service.domain.ReviewPlanDeletedQueryDomain;
import com.lotus.lqmp.service.domain.ReviewPlanQueryDomain;
import com.lotus.lqmp.service.service.ReviewPlanService;
import com.lotus.platform.common.biz.impl.BaseBiz;
import com.lotus.platform.common.model.enums.DeletedStatusEnum;
import com.lotus.platform.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 评审计划表Service业务层处理
 * @author Shiheng.Liu1
 * @date 2022-03-29
 */
@Service
@Slf4j
public class ReviewPlanServiceImpl extends BaseBiz<ReviewPlanMapper, ReviewPlanDo> implements ReviewPlanService {
    @Autowired
    private ReviewPlanMapper reviewPlanMapper;

    @Override
    public List<ReviewPlanQueryDomain> listForReviewPlanPage(ReviewPlanQueryReq queryInfo, long start, long pageSize) {
        return reviewPlanMapper.listForReviewPlanPage(queryInfo, start, pageSize);
    }

    @Override
    public Long countForReviewPlanPage(ReviewPlanQueryReq queryInfo) {
        return reviewPlanMapper.countForReviewPlanPage(queryInfo);
    }

    @Override
    public Long countNotGoingPlan(List<Long> ids, Integer status) {
        return reviewPlanMapper.countNotGoingPlan(ids, status);
    }

    @Override
    public void logicBatchDelete(List<Long> ids) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<ReviewPlanDo> reviewPlanDos = reviewPlanMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(reviewPlanDos)) {
            return;
        }

        reviewPlanDos.forEach(curr -> {
            curr.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
            curr.setDeletedStatus(UuidUtil.getUUID());
        });
        updateBatchById(reviewPlanDos);
    }

    @Override
    public List<ReviewPlanDeletedQueryDomain> listForDeletedReviewPlanPage(ReviewPlanQueryReq queryInfo, long start,
        long pageSize) {
        return reviewPlanMapper.listForDeletedReviewPlanPage(queryInfo, start, pageSize);
    }

    @Override
    public Long countForDeletedReviewPlanPage(ReviewPlanQueryReq queryInfo) {
        return reviewPlanMapper.countForDeletedReviewPlanPage(queryInfo);
    }

    @Override
    public List<ReviewPlanDo> getNotDeletedPlans(List<Long> ids) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted_status", String.valueOf(DeletedStatusEnum.normal.getCode()));
        queryWrapper.in("id", ids);
        return reviewPlanMapper.selectList(queryWrapper);
    }

    @Override
    public List<ReviewPlanDo> getDeletedPlans(List<Long> ids) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("deleted_status", String.valueOf(DeletedStatusEnum.normal.getCode()));
        queryWrapper.eq("manual_clear_status", 0);
        queryWrapper.in("id", ids);
        return reviewPlanMapper.selectList(queryWrapper);
    }

    @Override
    public void batchRestorePlan(List<Long> ids) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<ReviewPlanDo> reviewPlanDos = reviewPlanMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(reviewPlanDos)) {
            return;
        }

        reviewPlanDos.forEach(curr -> {
            curr.setDeletedStatus(String.valueOf(DeletedStatusEnum.normal.getCode()));
            curr.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
        });
        updateBatchById(reviewPlanDos);
    }

    @Override
    public void clearAll() {
        UpdateWrapper<ReviewPlanDo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("manual_clear_status", 0);
        updateWrapper.apply("deleted_status != {0}", String.valueOf(DeletedStatusEnum.normal.getCode()));
        updateWrapper.set("manual_clear_status", 1);
        update(updateWrapper);
    }

    @Override
    public ReviewPlanDo getNotDeletedByEcuInventoryId(Long ecuInventoryId) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted_status", String.valueOf(DeletedStatusEnum.normal.getCode()));
        queryWrapper.eq("ecu_inventory_id", ecuInventoryId);
        return reviewPlanMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ReviewPlanDo> getNotDeletedByEcuInventoryIds(List<Long> ecuInventoryIds) {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted_status", String.valueOf(DeletedStatusEnum.normal.getCode()));
        queryWrapper.in("ecu_inventory_id", ecuInventoryIds);
        return reviewPlanMapper.selectList(queryWrapper);
    }

    @Override
    public long getTodayCreatedNum() {
        QueryWrapper<ReviewPlanDo> queryWrapper = new QueryWrapper<>();
        try {
            queryWrapper.gt("create_time", DateUtils.parseDate(DateUtils.format(new Date(), DateUtils.shortFormat), DateUtils.shortFormat));
            return reviewPlanMapper.selectList(queryWrapper).size();
        } catch (ParseException e) {
            log.error("init today zero time error", e);
        }
        return 0L;
    }
}
